
/**
 * 

var cu = new ClickUpdate( divId , cuEl)


cu.addEventListener(eventName, handler)

cu.removeEventListener()

cu.dispatchEvent(eventName, jsonData)


事件列表:

complete => 对应之前的callback

ajaxError => 请求出错 

dataReady 数据准备好,模板未渲染
 */
var ClickUpdate = function(divId, cuEl){
	var reEl = /@(.+?)\((.+?)\)/,
	    args,
	    realArgs = [],
	    guid = 0,
	    blankFn = new Function,
	    urlPath;
	function _parseEl(el){
	    if(reEl.test(cuEl)){
	    	urlPath = RegExp.$1;
	    	args = RegExp.$2.replace(/ /g,'').split(',');
	    	
	        //alert('url:'+ urlPath + ' arg:' + args.join(','));    
	    }
	}
	// From tangram, thanks!
	function _toJson(jsonString){
        return (new Function("return " + jsonString))();
	}
	// From tangram, thanks!
	function _getXHR() {
        if (window.ActiveXObject) {
            try {
                return new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    return new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
            }
        }
        if (window.XMLHttpRequest) {
            return new XMLHttpRequest();
        }
    }
	function ajaxRequest(method, url, async, callback, data){
    	var xhr = _getXHR();
        xhr.open(method, url, async);
        xhr.onreadystatechange = function(){
            if(xhr.readyState == 4){
                callback.call(window, xhr.responseText);
                xhr.onreadystatechange = blankFn;
                async && (xhr = null);
            }
        }
        if (method == 'POST') {
             xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        }
        xhr.send(data);
	}
	_parseEl(cuEl);
    var fn = function(){
    	fn.__listeners = {};
    	realArgs.push.apply(realArgs, arguments);
    	//参数不足,返回本身
    	if(realArgs.length < args.length){
    		return fn;
    	}
    	// 处理ajax回传内容逻辑
    	function onAjaxComplete(responseText){
    	    //TODO 回调函数处理?
    	    cuEl = cuEl.replace(reEl, responseText);
    	    fn.dispatchEvent('ajaxComplete', responseText);
    	    onDataReady();
    	}
    	function onDataReady(){
            alert(cuEl);
    		fn.dispatchEvent('dataReady', _toJson(cuEl));
    	};
    	//TODO post,get? url规则? data?
    	ajaxRequest("post", urlPath.replace(/\./,'/'), true, onAjaxComplete, realArgs.join(','));
    };
    
    fn.addEventListener = function(type, handler){
        handler.hashCode = ++guid;
        type = type.toLowerCase();
        var listener = fn.__listeners;
        listener[type] = listener[type] || {};
        listener[type][guid] = handler;
    };
    fn.removeEventListener = function(type, handler){
    	type = type.toLowerCase();
    	var listener = fn.__listeners;
    	delete listener[type][handler.hashCode];
    };
    fn.dispatchEvent = function(type, data){
    	type = type.toLowerCase();
        var hdls = fn.__listeners[type], p;
        for(p in hdls){
        	try {
        	   typeof hdls[p] == 'function' && hdls[p].call(fn, data);
        	}catch(error){}
        }
    }
	
	return fn;
}
ClickUpdate.makeURL
ClickUpdate.makeData